package com.cskaoyan.shiro.service;

import com.cskaoyan.shiro.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName AdminServiceImpl
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/5/19 14:39
 * @Version V1.0
 **/
@Service
public class AdminServiceImpl implements AdminService{

    @Autowired
    AdminMapper adminMapper;

    @Override
    public String selectPasswordByUsername(String username) {
        return adminMapper.selectPasswordByUsername(username);
    }

    @Override
    public List<String> selectPermissionsByUsername(String username) {
        // [1]----取出里面的值 字符串替换 [   ]    ,分割
        String roleIds = adminMapper.selectRolesByUsername(username);
        //先将左边的[去掉
        roleIds = roleIds.replace("[", "");
        //再将后面的]去掉
        roleIds = roleIds.replace("]", "");
        String[] strings = roleIds.split(",");
        List<String> ids =  new ArrayList<>();
        if(strings != null){
            for (String rid : strings) {
                ids.add(rid);
            }
        }else {
            ids.add(roleIds);
        }
        List<String> perms = adminMapper.queryPermissionsByRoleIds(ids);
        return perms;
    }
}
